//
//  MessageViewController.m
//  PrintPort
//
//  Created by hy002 on 2018/1/3.
//  Copyright © 2018年 HUAYU. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageModel.h"
#import "MessageDetailViewController.h"

static NSString *cellPath = @"MessageCell";
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSMutableArray *messageAry;
@property (nonatomic, assign)BOOL isQianShou;
@property (nonatomic, assign)NSInteger page;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _messageAry = [NSMutableArray arrayWithCapacity:0];
    [self creatUI];
    [self requestMsgPush];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"消息通知";
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = BacksColor;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    
}

- (void)loadNewHeaderData
{
    _page = 0;
    [self requestMsgPush];
}

- (void)loadMoreFootData
{
    _page++;
    [self requestMsgPush];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_messageAry.count > 0)
    {
        if (indexPath.row != _messageAry.count-1)
        {
            UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 89*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
            lineview.backgroundColor = LinesColor;
            [cell.contentView addSubview:lineview];
        }
        MessageModel *model = _messageAry[indexPath.row];
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, SCREEN_WIDTH-30*displaywidth, 30*displaywidth) Font:14.0 Text:model.title Color:RGB(77, 77, 77)];
        titLab.lineBreakMode = NSLineBreakByTruncatingTail;
        [cell.contentView addSubview:titLab];
        UILabel *contLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH-30*displaywidth, 30*displaywidth) Font:12.0 Text:model.content Color:RGB(128, 128, 128)];
        contLab.lineBreakMode = NSLineBreakByTruncatingTail;
        [cell.contentView addSubview:contLab];
        UILabel *timeLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 60*displaywidth, SCREEN_WIDTH-130*displaywidth, 25*displaywidth) Font:12.0 Text:model.createDtm Color:RGB(128, 128, 128)];
        [cell.contentView addSubview:timeLab];
        UILabel *detailLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-115*displaywidth, 60*displaywidth, 100*displaywidth, 25*displaywidth) Font:12.0 Text:@"查看详情" Color:RGB(77, 77, 77)];
        detailLab.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:detailLab];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel *model = _messageAry[indexPath.row];
    MessageDetailViewController *vc = [[MessageDetailViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90*displaywidth;
}





- (void)requestMsgPush
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":s_Integer(_page)};
    [HYBaseHandler requestMsgPushParmers:dic Success:^(id obj)
    {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *msgAry = [MessageModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.page == 0)
                {
                    [weakSelf.messageAry removeAllObjects];
                }
                [weakSelf.messageAry addObjectsFromArray:msgAry];
                [weakSelf.tableview reloadData];
            });
            
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
